﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;

namespace ScrumMonster
{
    public class ScrumMonsterContext
    {
        private List<Project> _projects = new List<Project>();
        private Settings _settings;

        public Settings Settings
        {
            get
            {
                if (_settings == null)
                    _settings = new Settings();
                
                return _settings;
            }
        }

        /// <summary>
        /// Get the projects in this context
        /// </summary>
        public ReadOnlyCollection<Project> Projects
        {
            get
            {
                if (_projects == null || _projects.Count == 0)
                {
                    _projects = GetProjects();
                }

                return _projects.AsReadOnly();
            }
        }

        /// <summary>
        /// Create a new instance fot ScrumMonster
        /// </summary>
        /// <param name="projectsPath">The path to store the projects information files.</param>
        public ScrumMonsterContext(string projectsPath)
        {
            Settings.ProjectsPath = projectsPath;
        }

        /// <summary>
        /// Create a new project in the SrcumMonster context.
        /// </summary>
        /// <param name="name">The name of the project to be created.</param>
        /// <returns>The create project.</returns>
        public Project CreateProject(string name)
        {
            Project project = new Project(name, Guid.NewGuid());

            WriteToFile(project);
            
            _projects.Add(project);

            return project;
        }

        /// <summary>
        /// Get all the projects
        /// </summary>
        /// <returns>List of the porjects</returns>
        private List<Project> GetProjects()
        {
            _projects = new List<Project>();

            foreach (string filePath in Directory.GetFiles(Settings.ProjectsPath))
            {   
                Project project = ReadFromFile(filePath);
                _projects.Add(project);
            }

            return _projects;
        }

        internal void WriteToFile(Project project)
        {
            XDocument doc = new XDocument();
            doc.Add(project.XElement);

            doc.Save(Settings.GetFullPath(project.XmlFileName));
        }

        internal Project ReadFromFile(string path)
        {
            XDocument doc = XDocument.Load(path);
            return new Project(doc.Root);
        }


        //private static XDocument ReadXmlResource(string resourceName)
        //{
        //    Assembly current = Assembly.GetExecutingAssembly();
        //    XmlReader reader = XmlReader.Create(current.GetManifestResourceStream(resourceName));
        //    return XDocument.Load(reader);
        //}

        //private static void SaveXmlResource(string resourceName, XDocument doc)
        //{
        //    Assembly current = Assembly.GetExecutingAssembly();
        //    XmlWriter writer = XmlWriter.Create(current.GetManifestResourceStream(resourceName));            
        //    doc.Save(writer);           
        //    XmlReader reader = XmlReader.Create(current.GetManifestResourceStream(resourceName));
        //    XDocument doc2 = XDocument.Load(reader);
        //}
        
        /// <summary>
        /// Delete a project from the context
        /// </summary>
        /// <param name="project">The project to be deleted.</param>
        internal void DeleteFile(Project project)
        {
            File.Delete(Settings.GetFullPath(project.XmlFileName));

            Project proj = (from x in _projects
                           where x.GUID == project.GUID
                           select x).First();

            this._projects.Remove(proj);
        }
    }
}

